package at.socialconference.web.client.ui;

import java.util.List;

import at.socialconference.inter.connection.protocol.SocketProtocol;
import at.socialconference.inter.model.Conference;
import at.socialconference.inter.model.Place;
import at.socialconference.inter.model.Talk;
import at.socialconference.web.client.SCwebService;
import at.socialconference.web.client.util.AsynCallbackDeleteObject;
import at.socialconference.web.client.util.AsynCallbackInsertObject;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class AdminHome extends Composite implements ClickHandler{
	private FlexTable content;
	
	private ListBoxConferences lbConferences = new ListBoxConferences();
	private ListBoxTalks lbTalks = new ListBoxTalks();
	private ListBoxPlaces lbPlaces = new ListBoxPlaces();
	
	private Button btnShowConf = new Button("Show");
	private Button btnRefConf = new Button("Refresh");
	private Button btnDelConf = new Button("Delete");
	private Button btnAddConf = new Button("Add");
	
	private Button btnShowTalk = new Button("Show");
	private Button btnRefTalk = new Button("Refresh");
	private Button btnDelTalk = new Button("Delete");
	private Button btnAddTalk = new Button("Add");
	private Button btnAddTalkToConf = new Button("Add to Conf");
	
	private Button btnShowPlace = new Button("Show");
	private Button btnRefPlace = new Button("Refresh");
	private Button btnDelPlace = new Button("Delete");
	private Button btnAddPlace = new Button("Add");

	public void updateConferences() {
		SCwebService.Util.getInstance().getConferences(new AsyncCallbackGetConferences());
	}
	
	public void updateTalks() {
		SCwebService.Util.getInstance().getTalks(new AsyncCallbackGetTalks());
	}
	
	public void updatePlaces() {
		SCwebService.Util.getInstance().getPlaces(new AsyncCallbackGetPlaces());
	}
	
	public AdminHome() {
		content = new FlexTable();
		initWidget(content);
		
		lbConferences.setVisibleItemCount(10);
		lbPlaces.setVisibleItemCount(10);
		lbTalks.setVisibleItemCount(10);
		
		lbConferences.setWidth("400px");
		lbPlaces.setWidth("400px");
		lbTalks.setWidth("400px");
		
		btnAddConf.setWidth("75px");
		btnDelConf.setWidth("75px");
		btnRefConf.setWidth("75px");
		btnShowConf.setWidth("75px");
		btnAddTalk.setWidth("75px");
		btnAddTalkToConf.setWidth("75px");
		btnDelTalk.setWidth("75px");
		btnRefTalk.setWidth("75px");
		btnShowTalk.setWidth("75px");
		btnAddPlace.setWidth("75px");
		btnDelPlace.setWidth("75px");
		btnRefPlace.setWidth("75px");
		btnShowPlace.setWidth("75px");
		
		btnAddConf.addClickHandler(this);
		btnDelConf.addClickHandler(this);
		btnRefConf.addClickHandler(this);
		btnShowConf.addClickHandler(this);
		btnAddTalk.addClickHandler(this);
		btnAddTalkToConf.addClickHandler(this);
		btnDelTalk.addClickHandler(this);
		btnRefTalk.addClickHandler(this);
		btnShowTalk.addClickHandler(this);
		btnAddPlace.addClickHandler(this);
		btnDelPlace.addClickHandler(this);
		btnRefPlace.addClickHandler(this);
		btnShowPlace.addClickHandler(this);
		
		VerticalPanel vpConf = new VerticalPanel();
		vpConf.add(btnAddConf);
		vpConf.add(btnDelConf);
		vpConf.add(btnRefConf);
		vpConf.add(btnShowConf);
		
		VerticalPanel vpTalk = new VerticalPanel();
		vpTalk.add(btnAddTalk);
		vpTalk.add(btnAddTalkToConf);
		vpTalk.add(btnDelTalk);
		vpTalk.add(btnRefTalk);
		vpTalk.add(btnShowTalk);
		
		VerticalPanel vpPlace = new VerticalPanel();
		vpPlace.add(btnAddPlace);
		vpPlace.add(btnDelPlace);
		vpPlace.add(btnRefPlace);
		vpPlace.add(btnShowPlace);
		
		int i=-1;
		
		Label lblConf = new Label("Conferences in the Database:");
		lblConf.getElement().getStyle().setMargin(5, Unit.PX);
		content.setWidget(++i, 0, lblConf);
		content.getFlexCellFormatter().setRowSpan(i, 0, 1);
		content.setWidget(++i, 0, lbConferences);
		content.setWidget(i, 1, vpConf);
		
		Label lblTalk = new Label("Talks in the Database:");
		lblConf.getElement().getStyle().setMargin(10, Unit.PX);
		content.setWidget(++i, 0, lblTalk);
		content.getFlexCellFormatter().setRowSpan(i, 0, 1);
		content.setWidget(++i, 0, lbTalks);
		content.setWidget(i, 1, vpTalk);
		
		Label lblPlace = new Label("Places in the Database:");
		lblConf.getElement().getStyle().setMargin(10, Unit.PX);
		content.setWidget(++i, 0, lblPlace);
		content.getFlexCellFormatter().setRowSpan(i, 0, 1);
		content.setWidget(++i, 0, lbPlaces);
		content.setWidget(i, 1, vpPlace);

		updateConferences();
		updateTalks();
		updatePlaces();
	}

	@Override
	public void onClick(ClickEvent event) {
		//Conf buttons
		if (event.getSource().equals(btnAddConf)) {
			new AddConferencePopup(AdminHome.this);
		}
		else if (event.getSource().equals(btnDelConf)) {
			SCwebService.Util.getInstance().deleteObject(lbConferences.getSelectedConference(), new AsyncCallbackDeleteConference());
		}
		else if (event.getSource().equals(btnRefConf)) {
			updateConferences();
		}
		else if (event.getSource().equals(btnShowConf)) {
			if (lbConferences.getSelectedConference()!=null)
				new ShowConferencePopup(lbConferences.getSelectedConference());
			else 
				Window.alert("Select a conference");
		}
		//Talk buttons
		else if (event.getSource().equals(btnAddTalk)) {
			new AddTalkPopup(AdminHome.this);
		} 
		else if (event.getSource().equals(btnAddTalkToConf)) {
			Conference selectedConference = lbConferences.getSelectedConference();
			Talk selectedTalk = lbTalks.getSelectedTalk();
			if (selectedTalk!=null) {
				if (selectedConference != null) {
					selectedConference.getTalks().add(selectedTalk);
					SCwebService.Util.getInstance().updateObject(selectedConference, new AsyncCallbackInsertConference());
				}
				else {
					Window.alert("Select a conference");
				}
			}
			else {
				Window.alert("Select a talk");
			}
		}
		else if (event.getSource().equals(btnDelTalk)) {
			SCwebService.Util.getInstance().deleteObject(lbTalks.getSelectedTalk(), new AsyncCallbackDeleteTalk());
		}
		else if (event.getSource().equals(btnRefTalk)) {
			updateTalks();
		}
		else if (event.getSource().equals(btnShowTalk)) {
			if (lbTalks.getSelectedTalk()!=null)
				new ShowTalkPopup(lbTalks.getSelectedTalk());
			else 
				Window.alert("Select a talk");
		}
		//Place buttons
		else if (event.getSource().equals(btnAddPlace)) {
			new AddPlacePopup(AdminHome.this);
		}
		else if (event.getSource().equals(btnDelPlace)) {
			SCwebService.Util.getInstance().deleteObject(lbPlaces.getSelectedPlace(), new AsyncCallbackDeletePlace());
		}
		else if (event.getSource().equals(btnRefPlace)) {
			updatePlaces();
		}
		else if (event.getSource().equals(btnShowPlace)) {
			if (lbPlaces.getSelectedPlace()!=null)
				new ShowPlacePopup(lbPlaces.getSelectedPlace());
			else 
				Window.alert("Select a place");
		}
	}
	
	private class AsyncCallbackDeleteConference extends AsynCallbackDeleteObject {
		@Override
		public void onSuccess(Integer result) {
			super.onSuccess(result);
			if (result==SocketProtocol.OK) {
				updateConferences();
			}
		}
	}
	
	private class AsyncCallbackDeleteTalk extends AsynCallbackDeleteObject {
		@Override
		public void onSuccess(Integer result) {
			super.onSuccess(result);
			if (result==SocketProtocol.OK) {
				updateTalks();
			}
		}
	}
	
	private class AsyncCallbackDeletePlace extends AsynCallbackDeleteObject {
		@Override
		public void onSuccess(Integer result) {
			super.onSuccess(result);
			if (result==SocketProtocol.OK) {
				updatePlaces();
			}
		}
	}

	private class AsyncCallbackGetConferences implements AsyncCallback<List<Conference>> {
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("Error getConfs OnFailure: " + caught.getMessage());
		}
		@Override
		public void onSuccess(List<Conference> confs) {
			AdminHome.this.lbConferences.clear();
			for (int i = 0; i < confs.size(); i++) {
				AdminHome.this.lbConferences.addConference(confs.get(i));
			}

		}
	}
	
	private class AsyncCallbackGetTalks implements AsyncCallback<List<Talk>> {
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("Error getTalks OnFailure: " + caught.getMessage());
		}
		@Override
		public void onSuccess(List<Talk> talks) {
			AdminHome.this.lbTalks.clear();
			for (int i = 0; i < talks.size(); i++) {
				AdminHome.this.lbTalks.addTalk(talks.get(i));
			}
		}
	}
	
	private class AsyncCallbackGetPlaces implements AsyncCallback<List<Place>> {
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("Error getPlaces OnFailure: " + caught.getMessage());
		}
		@Override
		public void onSuccess(List<Place> places) {
			AdminHome.this.lbPlaces.clear();
			for (int i = 0; i < places.size(); i++) {
				AdminHome.this.lbPlaces.addPlace(places.get(i));
			}
		}
	}
	
	private class AsyncCallbackInsertConference extends AsynCallbackInsertObject {
		@Override
		public void onSuccess(Integer result) {
			super.onSuccess(result);
			if (result==SocketProtocol.OK) {
				updateConferences();
			}
		}
	}
}
